﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input;
using System.IO;
using System.IO.IsolatedStorage;

namespace DungDungShit
{
    class Room
    {
        public IntVector2 position;
        public bool[] open = new bool[4];
        public bool[,] link = new bool[4,4];
        public IntVector2 unitIndex;
        public Tile[,] map = null;

        public Room(IntVector2 pos, IntVector2 unitIndex)
        {
            position = pos;
            for (int i = 0; i < 4; i++)
            {
                open[i] = false;
                for (int j = 0; j < 4; j++)
                {
                    link[i,j] = false;
                }
            }
            this.unitIndex = unitIndex;
        }
        public Room(IntVector2 pos, bool left, bool right, bool up, bool down, bool[,] link)
        {
            position = pos;
            open[RoomEntrance.left] = left;
            open[RoomEntrance.right] = right;
            open[RoomEntrance.up] = up;
            open[RoomEntrance.down] = down;
            this.link = link;
        }

        private string BoolToString(bool input)
        {
            if (input)
                return "1";
            else
                return "0";
        }
        public void SelectPreset(ScreenManager screenManager)
        {
            if (link[RoomEntrance.left, RoomEntrance.right])
            {
                if (link[RoomEntrance.up, RoomEntrance.left] && link[RoomEntrance.down, RoomEntrance.left])
                {
                    link[RoomEntrance.up, RoomEntrance.down] = true;
                    link[RoomEntrance.down, RoomEntrance.up] = true;
                    link[RoomEntrance.up, RoomEntrance.right] = true;
                    link[RoomEntrance.down, RoomEntrance.right] = true;
                }
            }
            if (link[RoomEntrance.up, RoomEntrance.down])
            {
                if (link[RoomEntrance.up, RoomEntrance.left] && link[RoomEntrance.up, RoomEntrance.right])
                {
                    link[RoomEntrance.left, RoomEntrance.right] = true;
                    link[RoomEntrance.right, RoomEntrance.left] = true;
                }
            }

            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (link[i, j])
                        link[j, i] = true;
                }
            }

            string key = BoolToString(open[RoomEntrance.up]) + BoolToString(open[RoomEntrance.down]) + BoolToString(open[RoomEntrance.left]) + BoolToString(open[RoomEntrance.right]);
            key = key + BoolToString(link[RoomEntrance.up, RoomEntrance.down]) + BoolToString(link[RoomEntrance.up, RoomEntrance.left]) + BoolToString(link[RoomEntrance.up, RoomEntrance.right]) + BoolToString(link[RoomEntrance.down, RoomEntrance.left]) + BoolToString(link[RoomEntrance.down, RoomEntrance.right]) + BoolToString(link[RoomEntrance.left, RoomEntrance.right]);
            //key값을 이용해 해당하는 프리셋을 찾아서 그 중 하나를 랜덤하게 선택한다
            DirectoryInfo di = new DirectoryInfo("Presets");
            FileInfo[] fi = di.GetFiles(key + "*.txt");
            LoadFromFile(key + "_" + ScreenManager.r.Next(fi.Length).ToString() + ".txt", screenManager);
        }
        public void SelectPreset(ScreenManager screenManager, bool isBase)
        {
            LoadFromFile("base.txt", screenManager);
        }

        public void LoadFromFile(string filename, ScreenManager screenManager)
        {
            
            FileStream fs = new FileStream("Presets\\" + filename, FileMode.Open);
            if (fs != null)
            {
                StreamReader reader = new StreamReader(fs);

                LoadMap(reader, screenManager);

                reader.Close();
                fs.Close();
            }
        }

        private void LoadMap(StreamReader reader, ScreenManager screenManager)
        {
            String[] size;
            int i, j;

            size = reader.ReadLine().Split(' ');
            int mapWidth = int.Parse(size[0]);
            int mapHeight = int.Parse(size[1]);

            // tiles
            map = new Tile[mapWidth, mapHeight];
            for (j = 0; j < mapHeight; j++)
            {
                String line = reader.ReadLine();
                for (i = 0; i < mapWidth; i++)
                {
                    SetMapTile(new IntVector2(i, j), screenManager, i < line.Length ? line[i] : ' ');
                }
            }
        }

        public void SetMapTile(IntVector2 v, ScreenManager screenManager, char tileKind, int speed = 0)
        {
            IntVector2 wallSheetSize = new IntVector2(1, 1);  // 벽 타일 움직임 세트 크기 : 6x1
            IntVector2 spikeSheetSize = new IntVector2(16, 1);
            IntVector2 fireSheetSize = new IntVector2(3, 1);
            if (!(v.X >= 0 && v.X < map.GetLength(0) && v.Y >= 0 && v.Y < map.GetLength(1)))
                return;
            Texture2D texture = screenManager.GetTextureFromChar(tileKind);

            IntVector2 defaultFrameSize = new IntVector2(Sprite.TileSize.X, Sprite.TileSize.Y);
            IntVector2 defaultSheetSize = new IntVector2(1, 1);

            switch (tileKind)
            {
                case 'E':
                    map[v.X, v.Y] = new EscapeTile(texture, v, defaultFrameSize, defaultSheetSize);
                    break;
                case '@':
                    int tid = ScreenManager.r.Next(200);
                    if (tid < 25)
                        tid = 0;
                    else if (tid < 45)
                        tid = 1;
                    else if (tid < 80)
                        tid = 2;
                    else if (tid < 100)
                        tid = 3;
                    else
                    {
                        map[v.X, v.Y] = new NormalTile(texture, v, defaultFrameSize, defaultSheetSize);
                        break;
                    }
                    map[v.X, v.Y] = new PersonTile(texture, screenManager.PeopleTexture(tid), v, defaultFrameSize, defaultSheetSize, tid);
                    break;
                case '$':
                    map[v.X, v.Y] = new FireTile(texture, v, defaultFrameSize, fireSheetSize);
                    break;
                case '^':
                    map[v.X, v.Y] = new SpikeTile(texture, v, defaultFrameSize, spikeSheetSize);
                    break;
                case '1':
                    map[v.X, v.Y] = new WallTile(texture, v, defaultFrameSize, wallSheetSize);
                    break;
                default:
                    map[v.X, v.Y] = new NormalTile(texture, v, defaultFrameSize, defaultSheetSize);
                    break;
            }
        }

    }
}
